#!/usr/bin/env node

/**
 * 测试本地地理位置数据功能
 * 运行: node scripts/test-local-location.js
 */

const path = require('path');
const fs = require('fs');

async function testLocalLocationData() {
    console.log('🧪 开始测试本地地理位置数据功能...\n');

    try {
        // 1. 测试本地数据文件是否存在
        console.log('1️⃣ 检查本地数据文件...');
        const dataDir = path.join(__dirname, '..', 'src', 'province-city-china');

        if (!fs.existsSync(dataDir)) {
            throw new Error(`数据目录不存在: ${dataDir}`);
        }
        console.log('✅ 数据目录存在');

        const provinceFile = path.join(dataDir, 'province.json');
        const cityFile = path.join(dataDir, 'city.json');
        const areaFile = path.join(dataDir, 'area.json');

        if (!fs.existsSync(provinceFile)) {
            throw new Error(`省份数据文件不存在: ${provinceFile}`);
        }
        if (!fs.existsSync(cityFile)) {
            throw new Error(`城市数据文件不存在: ${cityFile}`);
        }
        if (!fs.existsSync(areaFile)) {
            throw new Error(`区域数据文件不存在: ${areaFile}`);
        }
        console.log('✅ 所有数据文件都存在');

        // 2. 测试数据文件内容
        console.log('\n2️⃣ 检查数据文件内容...');

        const provinceData = JSON.parse(fs.readFileSync(provinceFile, 'utf-8'));
        const cityData = JSON.parse(fs.readFileSync(cityFile, 'utf-8'));
        const areaData = JSON.parse(fs.readFileSync(areaFile, 'utf-8'));

        console.log(`✅ 省份数据: ${provinceData.length} 条记录`);
        console.log(`✅ 城市数据: ${cityData.length} 条记录`);
        console.log(`✅ 区域数据: ${areaData.length} 条记录`);

        // 3. 测试数据结构
        console.log('\n3️⃣ 检查数据结构...');

        if (provinceData.length > 0) {
            const sampleProvince = provinceData[0];
            console.log('✅ 省份数据结构示例:', {
                code: sampleProvince.code,
                name: sampleProvince.name,
                province: sampleProvince.province
            });
        }

        if (cityData.length > 0) {
            const sampleCity = cityData[0];
            console.log('✅ 城市数据结构示例:', {
                code: sampleCity.code,
                name: sampleCity.name,
                province: sampleCity.province,
                city: sampleCity.city
            });
        }

        if (areaData.length > 0) {
            const sampleArea = areaData[0];
            console.log('✅ 区域数据结构示例:', {
                code: sampleArea.code,
                name: sampleArea.name,
                province: sampleArea.province,
                city: sampleArea.city,
                area: sampleArea.area
            });
        }

        // 4. 测试数据关联性
        console.log('\n4️⃣ 检查数据关联性...');

        const beijingProvince = provinceData.find(p => p.name === '北京市');
        if (beijingProvince) {
            const beijingCities = cityData.filter(c => c.province === beijingProvince.province);
            console.log(`✅ 北京市下城市数量: ${beijingCities.length}`);

            if (beijingCities.length > 0) {
                const beijingCity = beijingCities[0];
                const beijingAreas = areaData.filter(a =>
                    a.province === beijingCity.province && a.city === beijingCity.city
                );
                console.log(`✅ 北京市下区域数量: ${beijingAreas.length}`);
            }
        }

        // 5. 测试数据完整性
        console.log('\n5️⃣ 检查数据完整性...');

        let totalCities = 0;
        let totalAreas = 0;

        for (const province of provinceData) {
            const cities = cityData.filter(c => c.province === province.province);
            totalCities += cities.length;

            for (const city of cities) {
                const areas = areaData.filter(a =>
                    a.province === city.province && a.city === city.city
                );
                totalAreas += areas.length;
            }
        }

        console.log(`✅ 总城市数量: ${totalCities}`);
        console.log(`✅ 总区域数量: ${totalAreas}`);

        console.log('\n🎉 本地地理位置数据测试完成！');
        console.log('\n📊 数据统计:');
        console.log(`   省份: ${provinceData.length}`);
        console.log(`   城市: ${totalCities}`);
        console.log(`   区域: ${totalAreas}`);
        console.log(`   数据来源: 本地文件 (${dataDir})`);

    } catch (error) {
        console.error('❌ 测试失败:', error.message);
        process.exit(1);
    }
}

// 运行测试
if (require.main === module) {
    testLocalLocationData().catch(console.error);
}

module.exports = { testLocalLocationData };
